package com.ccrfid.rmc6.resource.device;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.device.CardreaderEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.device.CardreaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Path("cardreaders")
@Produces(MediaType.APPLICATION_JSON)
public class CardreaderResource {
    private static final Logger logger = LoggerFactory.getLogger(CardreaderResource.class);
    @Autowired
    private CardreaderService cardreaderService;

    @GET
    @Path("search")
    public Page<CardreaderEntity> getCardreaderInfoPage(
            @QueryParam("sn") String sn,
            @QueryParam("enabled") Integer enabled,
            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (sn != null) {
            criteria.add(new Criterion("sn", Criterion.Operator.LIKE, "%" + sn + "%"));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }

        Page<CardreaderEntity> result = cardreaderService.getCardreaderInfoPage(criteria, pageRequest);
        return result;
    }

    @GET
    @Path("{readerId}")
    public CardreaderEntity getCardreader(@PathParam("readerId") Long readerId) throws AppException {
        CardreaderEntity cardreaderEntity = cardreaderService.getCardreaderById(readerId);
        return cardreaderEntity;
    }

    @PUT
    @Path("{readerId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public CardreaderEntity updateCardreader(@PathParam("readerId") Long readerId, CardreaderEntity entity)
            throws AppException {
        CardreaderEntity cardreaderEntity = cardreaderService.updateCardreaderEntity(readerId, entity);

        return cardreaderEntity;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public CardreaderEntity createCardreader(CardreaderEntity entity) throws AppException {
        CardreaderEntity cardreaderEntity = cardreaderService.createCardreaderEntity(entity);
        return cardreaderEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteCardreadersInfo(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return cardreaderService.deleteCardreadersInfo(idList);
    }

}
